package com.lkpstudy.rbac.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lkpstudy.rbac.entity.User;
import com.lkpstudy.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author likaiping
 * @editTime 2024/4/18 14:25
 */
public class BaseController<S extends IService,E> {
    @Autowired
    protected S baseService;
    @GetMapping("/{id}")
    public E get(@PathVariable("id") Long id) {
        return (E) baseService.getById(id);
    }

    @GetMapping("/{page}/{size}")
    public IPage<E> page(@PathVariable("page") int page, @PathVariable("size") int size) {
        return baseService.page(new Page<>(page, size));
    }

    @PostMapping
    public E plus(@RequestBody E user) {
        if (baseService.save(user)) {
            return user;
        }
        return null;
    }

    @DeleteMapping("/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        if (baseService.removeById(id)) {
            return true;
        }
        return null;
    }

    @PutMapping
    public Boolean edit(@RequestBody E user) {
        if (baseService.updateById(user)) {
            return true;
        }
        return null;
    }
}
